package com.kevin.design.behavioralPattern.observerPattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 观察者模式
 * @Author Kevin
 * @Date: Created in  14:42 2020/1/16:
 * @Modified By:
 * @Email: tang.lx@topcheer.com
 */
public class ObserverPatternTest {
    public static void main(String[] args) {
        BObserver bObserver = new BObserver();
        Observer observer1 = new Observer1("a");
        Observer observer2 = new Observer1("b");
        Observer observer3 = new Observer1("c");
        bObserver.registerOb(observer1);
        bObserver.registerOb(observer2);
        bObserver.registerOb(observer3);
        bObserver.notifyAlls();
    }
}

interface Observer{
    public void handler();
}

class Observer1 implements Observer{

    private String name;

    public Observer1(String name) {
        this.name = name;
    }

    @Override
    public void handler() {
        System.out.println(name+"要变身了！");
    }
}

class BObserver{
    private List<Observer> observers = new ArrayList<Observer>();

    public void registerOb(Observer observer) {
        observers.add(observer);
    }

    public void notifyAlls() {
        for (Observer observer:observers) {
            observer.handler();
        }
    }
}
